package com.snopy.designPattern.chainOfResponsibility;

import com.snopy.designPattern.chainOfResponsibility.impl.RoleCheckMiddleware;
import com.snopy.designPattern.chainOfResponsibility.impl.Server;
import com.snopy.designPattern.chainOfResponsibility.impl.ThrottlingMiddleware;
import com.snopy.designPattern.chainOfResponsibility.impl.UserExistsMiddleware;
import com.snopy.designPattern.chainOfResponsibility.inter.Middleware;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author snopy
 * @version 1.0.0
 * @classname MiddlewareDemo
 * @description
 * @email 77912204@qq.com
 * @date 2022/03/04 13:26
 */
@Slf4j
public class MiddlewareDemo {
    private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    private static Server server;
    private static void init(){
        server = new Server();
        server.register("admin@gmail.com","123456");
        server.register("user@gmail.com","123456");
        Middleware middleware = new ThrottlingMiddleware(2);
        middleware.linkWith(new UserExistsMiddleware(server)).linkWith(new RoleCheckMiddleware());
        server.setMiddleware(middleware);
    }
    public static void main(String[] args) {
        init();
        boolean success = false;
        try {
            while (!success){
                System.out.print("Enter username:");
                String username = bufferedReader.readLine();
                System.out.print("Enter password:");
                String password = bufferedReader.readLine();
                success = server.login(username,password);
                System.out.println("");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
